<template>
  <div>
   <!--充值数据同统计卡片-->
   <div class="cardV1" v-bind:style="{ border: '1px solid #ccc' }">
       
  </div>
   <div class="cardV2" v-bind:style="{ border: '1px solid #ccc' }">
       
   </div>
    <div class="gva-search-box">
      <el-form ref="elSearchFormRef" :inline="true" :model="searchInfo" class="demo-form-inline" :rules="searchRule" @keyup.enter="onSubmit">
      <el-form-item label="日期范围" prop="createdAt">
      <template #label>
        <span>
          日期范围1
          <el-tooltip content="搜索范围是开始日期（包含）至结束日期（不包含）">
            <el-icon><QuestionFilled /></el-icon>
          </el-tooltip>
        </span>
      </template>
      <el-date-picker v-model="searchInfo.startCreatedAt" type="datetime" placeholder="开始日期" :disabled-date="time=> searchInfo.endCreatedAt ? time.getTime() > searchInfo.endCreatedAt.getTime() : false"></el-date-picker>
       —
      <el-date-picker v-model="searchInfo.endCreatedAt" type="datetime" placeholder="结束日期" :disabled-date="time=> searchInfo.startCreatedAt ? time.getTime() < searchInfo.startCreatedAt.getTime() : false"></el-date-picker>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="search" @click="onSubmit">查询</el-button>
          <el-button icon="refresh" @click="onReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
        <div class="gva-btn-list">

        </div>
        <el-table
        ref="multipleTable"
        style="width: 100%"
        tooltip-effect="dark"
        :data="tableData"
        row-key="ID"
        >
        <el-table-column align="left" label="日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.createTime) }}</template>
         </el-table-column>        
        <el-table-column align="left" label="充值金额" prop="name" width="120" />
        <el-table-column align="left" label="新用户充值" prop="author" width="120" />
        <el-table-column align="left" label="老用户充值" prop="totalCnt" width="120" />
        <el-table-column align="left" label="首次充值" prop="maxFreeCnt" width="120" />
        <el-table-column align="left" label="普通充值" prop="price" width="120" />
        <el-table-column align="left" label="普通充值支付订单数" prop="fullFlag" width="120" />
        <el-table-column align="left" label="VIP充值" prop="vip" width="120" />
        <el-table-column align="left" label="VIP会员支付订单数" prop="vip" width="120" />


         <el-table-column align="left" label="创建时间" width="180">
            <template #default="scope">{{ formatDate(scope.row.createTime) }}</template>
         </el-table-column>

        </el-table>
        <div class="gva-pagination">
            <el-pagination
            layout="total, sizes, prev, pager, next, jumper"
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            />
        </div>
    </div>

  </div>
</template>

<script setup>
import {
  findMovieAuto,
  getMovieAutoList
} from '@/api/movieAuto'
import { getUrl } from '@/utils/image'
// 图片选择组件
import SelectImage from '@/components/selectImage/selectImage.vue'

// 生成推广链接组件
import ReferralForm from '@/view/tReferral/tReferralForm.vue'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict, ReturnArrImg, onDownloadFile } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref, reactive } from 'vue'

//引用菜单按钮权限组件
import { useBtnAuth } from '@/utils/btnAuth'
    const btnAuth = useBtnAuth()

defineOptions({
    name: 'MovieAuto'
})

// 自动化生成的字典（可能为空）以及字段
const finishStatusOptions = ref([])
const shelvesOptions = ref([])
const formData = ref({
        name: '',
        imgUrl: "",
        bannerImgUrl: "",
        author: '',
        totalCnt: 0,
        maxFreeCnt: 0,
        price: 0,
        fullFlag: undefined,
        cpId: 0,
        cpUpdateTime: new Date(),
        protagonist: '',
        tag: '',
        intro: '',
        recommend: '',
        remarks: '',
        status: undefined,
        firstSid: 0,
        latestEpisode: '',
        createUid: 0,
        createTime: new Date(),
        updateUid: 0,
        updateTime: new Date(),
        })


// 验证规则
const rule = reactive({          
})

const searchRule = reactive({
  createdAt: [
    { validator: (rule, value, callback) => {
      if (searchInfo.value.startCreatedAt && !searchInfo.value.endCreatedAt) {
        callback(new Error('请填写结束日期'))
      } else if (!searchInfo.value.startCreatedAt && searchInfo.value.endCreatedAt) {
        callback(new Error('请填写开始日期'))
      } else if (searchInfo.value.startCreatedAt && searchInfo.value.endCreatedAt && (searchInfo.value.startCreatedAt.getTime() === searchInfo.value.endCreatedAt.getTime() || searchInfo.value.startCreatedAt.getTime() > searchInfo.value.endCreatedAt.getTime())) {
        callback(new Error('开始日期应当早于结束日期'))
      } else {
        callback()
      }
    }, trigger: 'change' }
  ],
})

const elFormRef = ref()
const elSearchFormRef = ref()

// =========== 表格控制部分 ===========
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})

// 重置
const onReset = () => {
  searchInfo.value = {}
  getTableData()
}

// 搜索
const onSubmit = () => {
  elSearchFormRef.value?.validate(async(valid) => {
    if (!valid) return
    page.value = 1
    pageSize.value = 10
    getTableData()
  })
}

// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async() => {
  const table = await getMovieAutoList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
const setOptions = async () =>{
    finishStatusOptions.value = await getDictFunc('finishStatus')
    shelvesOptions.value = await getDictFunc('shelves')
}

// 获取需要的字典 可能为空 按需保留
setOptions()




// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateMovieAutoFunc = async(row) => {
    const res = await findMovieAuto({ ID: row.ID })
    type.value = 'update'
    if (res.code === 0) {
        formData.value = res.data.removie
        dialogFormVisible.value = true
    }
}

</script>

<style>
   .cardV1{
      width: 325px;
      height: 135px;
      border-radius: 10px;
      background-color: #6f9dfb;
   }

   .cardV2 {
      width: 325px;
      height: 135px;
      border-radius: 10px;
      position: relative;
      bottom: 15px;
      background-color: #ffffff;
   }

</style>
